<%-- 
    Document   : getCompleteRoute
    Created on : Aug 25, 2009, 10:16:34 AM
    Author     : david
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"
        import="com.appspot.nnaplanner.*,
        java.util.List,
        java.util.ArrayList,
        java.util.Set,
        com.google.appengine.api.datastore.Query,
        com.google.appengine.api.datastore.DatastoreServiceFactory,
        com.appspot.nnaplanner.DAL.*,
        java.util.logging.Logger,
        java.text.DateFormat,
        java.text.SimpleDateFormat,
        java.util.Date,
        java.lang.StringBuilder
        "%>
<%
//test URL : http://localhost:8080/getCompleteRoute.jsp?
//startLat=51.48448304&startLon=-2.678260803&endLat=51.456594&endLon=-2.60427&offset=0.008&time=8:16&range=20
//startlat=51.486941636341456&endlat=51.463205362481744
            try {
                StringBuilder output = new StringBuilder(2000);
                float startLat = 0;
                float startLon = 0;
                float endLat = 0;
                float endLon = 0;
                float offset = 0;
                String time = "8:00";
                int range = 0;
                try {
                    startLat = 0 + Float.parseFloat(request.getParameter("startLat"));
                    startLon = 0 + Float.parseFloat(request.getParameter("startLon"));
                    endLat = 0 + Float.parseFloat(request.getParameter("endLat"));
                    endLon = 0 + Float.parseFloat(request.getParameter("endLon"));
                    offset = 0 + Float.parseFloat(request.getParameter("offset"));
                    range = Integer.parseInt(request.getParameter("range"));
                    if (request.getParameter("time") != null) {
                        time = request.getParameter("time");
                        
                    }
                    if (offset > 0.009) {
                        offset = 0.009f;
                    }
                } catch (Exception ex) {
                    out.print("{\"total\":\"0\", \"msg\":\"Error: Wrong parameters\", \"err:\"" + ex.toString() + "\"}");
                    return;
                }


                List<DALStop> nearStart = Utils.getStopsNear(startLat, startLon, offset);
                //out.print("<br>start geo " + startLat + " " + startLon);
                List<DALStop> nearEnd = Utils.getStopsNear(endLat, endLon, offset);
                //out.print("<br>start geo " + endLat + " " + endLon);
                List<DALConnection> route = new ArrayList<DALConnection>();
                int loop = 0;
                while (route.size() < 1) {
                    double distance = 100;
                    double tmpDistance = 0;
                    DALStop closestStartStop = new DALStop();
                    DALStop closestEndStop = new DALStop();

                    for (DALStop ds : nearStart) {
                        tmpDistance = Utils.getDistanceBetween(startLat, startLon, ds.getLatitude(), ds.getLongitude());
                        if (tmpDistance < distance) {
                            distance = tmpDistance;
                            closestStartStop = ds;
                        }
                    }

                    distance = 100;
                    tmpDistance = 0;
                    for (DALStop ds : nearEnd) {
                        tmpDistance = Utils.getDistanceBetween(endLat, endLon, ds.getLatitude(), ds.getLongitude());
                        if (tmpDistance < distance) {
                            distance = tmpDistance;
                            closestEndStop = ds;
                        }
                    }

                    route = NNappData.getRoute(closestStartStop.getID(), closestEndStop.getID());
                    nearStart.remove(closestStartStop);
                    nearEnd.remove(closestEndStop);
                    loop++;
                    if (loop > 10) {
                        Logger.getAnonymousLogger().warning("Waring: Looking for too many routes.Overloading server");
                        break;
                    }
                }
                DateFormat df = new SimpleDateFormat("HH:mm");
                Date depTime = df.parse(time);
                List<DALConnection> optimizedRoute = Utils.getOptimizedRoute(route, depTime, range);

                int innercount = 0;
                int count = 0;
                output.append("{\"total\": " + optimizedRoute.size() + ", \"conns\": [");
                for (DALConnection d : optimizedRoute) {
                    DALStop start = new DALStop().getByID(d.getStopID());
                    DALStop end = new DALStop().getByID(d.getDestinationID());
                    if (count > 0) {
                        output.append(", ");
                    }
                    output.append("{");
                    output.append("\"conid\": \"" + d.getID() + "\", ");
                    output.append("\"stopid\": \"" + d.getStopID() + "\", ");
                    output.append("\"sname\": \"" + d.getStopName() + "\", ");
                    output.append("\"sdesc\": \"" + start.getDescription() + "\", ");
                    output.append("\"slatitude\": \"" + start.getLatitude() + "\", ");
                    output.append("\"slongitude\": \"" + start.getLongitude() + "\", ");
                    output.append("\"depid\": \"" + d.getDestinationID() + "\", ");
                    output.append("\"dname\": \"" + d.getDestinationName() + "\", ");
                    output.append("\"ddesc\": \"" + end.getDescription() + "\", ");
                    output.append("\"dlatitude\": \"" + end.getLatitude() + "\", ");
                    output.append("\"dlongitude\": \"" + end.getLongitude() + "\", ");
                    if (d.getTimes() != null) {
                        output.append("\"departures\": \"" + d.getTimes().size() + "\", \"times\":[");
                        innercount = 0;
                        for (int i = 0; i < d.getTimes().size(); i++) {
                            if (innercount > 0) {
                                out.print(", ");
                            }
                            output.append("{");
                            output.append("\"time\": \"" + d.getTimes().get(i).toString() + "\", ");
                            output.append("\"duration\": \"" + d.getDurations().get(i).toString() + "\", ");
                            output.append("\"desc\": \"" + d.getDescriptions().get(i).toString() + "\" ");
                            output.append("}");
                            innercount++;
                        }
                        output.append("] ");

                    } else {
                        output.append("\"departures\": 0");
                    }
                    output.append("}");
                    count++;
                }
                output.append("]}");
                out.print(output.toString());
            } catch (Exception e) {
                Logger.getAnonymousLogger().warning("Could not get route \n" + e.toString());
            }
%>